layui.define(function(exports){ //提示：模块也可以依赖其它模块
var mue = {
    
    //左部主菜单
    leftrender:function($this, options){
        var that=this;
        var _thisgrid = $this;
        //获取表格参数
        var _thisOptions = options ? options : "";
        if (!_thisOptions) {
            layer.alert("请设置参数数据!");
            return;
        }
        //数据地址
        var dataurl = _thisOptions.dataurl ? _thisOptions.dataurl : '';
        //id名称
        var idname = _thisOptions.idname ? _thisOptions.idname : false;
        //pid名称
        var pidname = _thisOptions.pidname ? _thisOptions.pidname : false;
        //is_top名称
        var is_top = _thisOptions.is_top ? _thisOptions.is_top : 0;
        //判断
        if(!idname && !pidname){
            layer.alert("请设置id及父id字段名!");
            return;
        }
        //获取数据
        data = that.getDataByUrl(dataurl);
        if(!data){
           // layer.alert("数据为空!")
           return
        }else{
           val=that.dealdata(data,idname,pidname,is_top);
           val.forEach((v, i) => {
             that.createLi(v,idname,pidname,_thisgrid,i);
            })
        }

        $("ul#"+_thisgrid).on('click',"li",function(e){
            $("ul#"+ _thisgrid+" li").removeClass('layui-nav-itemed');
            $(this).addClass('layui-nav-itemed');
         })

    },
    //顶部菜单
    toprender:function($this, options){
        var that=this;
        var _thisgrid = $this;
        //获取表格参数
        var _thisOptions = options ? options : "";
        if (!_thisOptions) {
            layer.alert("请设置参数数据!");
            return;
        }
        //数据地址
        var dataurl = _thisOptions.dataurl ? _thisOptions.dataurl : '';
        //id名称
        var idname = _thisOptions.idname ? _thisOptions.idname : false;
        //pid名称
        var pidname = _thisOptions.pidname ? _thisOptions.pidname : false;
        //is_top名称
        var is_top = _thisOptions.is_top ? _thisOptions.is_top : 1;
        //判断
        if(!idname && !pidname){
            layer.alert("请设置id及父id字段名!");
            return;
        }
        //获取数据
        data = that.getDataByUrl(dataurl);
        if(!data){
           // layer.alert("数据为空!")
           return
        }else{
           val=that.dealdata(data,idname,pidname,is_top);
           val.forEach((v, i) => {
             that.createtopLi(v,idname,pidname,_thisgrid,i);
            })
        }
        
        $("ul#"+_thisgrid).on('mouseover',"li",function(e){
            $("ul#"+ _thisgrid+" li").removeClass('layui-nav-itemed');
            $(this).addClass('layui-nav-itemed');
        });
        $("ul#"+_thisgrid).on('mouseout',"li",function(e){
            $("ul#"+ _thisgrid+" li").removeClass('layui-nav-itemed');
        });
    },
    //获取数据
    getDataByUrl : function(dataurl) {
        if (dataurl != undefined && dataurl != "") {
          var data='';
            $.ajax({
                url: dataurl,
                async:false,
                type: 'get',
                dataType: "json",
                success:function(R){
                if (R.code == 200) {
                    data = R.data;
                } else {
                     data = {};
                }

                },
                error: function (R) {
                  // alert('failure to send to database');
                }
            });
            return data;
        } else {
            layer.alert("请设置数据加载路径!");
            return;
        }
    },

    // 先把数据处理成map
    dealdata:function(data,idname,pidname,is_top){
        // 数据
        const map = {};
        const val = [];
        data.forEach((item) => {
         if(item.is_top==is_top){
           map[item[idname]] = item;  
         }
        });
       // 2、遍历之后，形成树形结构
        data.forEach((item,i) => {
        if(item.is_top==is_top){
        const parent = map[item[pidname]];
          if (parent) {
               (parent.children || (parent.children = [])).push(item);
             } else {
                val.push(item);
             }
            }
         });
         return val;
    },
    //创建树形布局
    createLi:function(data,idname,pidname,thisgrid,i) {
            // 父元素id
            const fid = thisgrid;
            const div = $('#'+fid);
            var content='';
            if(data[pidname]==0){
                content +='<a href="'+data.url+'" lay-tips="'+data.title+'">';
                if(data.icon){
                content +='<i class="layui-icon">&#'+data.icon+'</i>';
                }
                content +='<cite>'+data.title+'</cite><i class="layui-icon layui-icon-down layui-nav-more"></i></a>';
            };
            var childrendiv=$('<li></li>'); //创建一个div
            childrendiv.attr('id', `mue${data[idname]}`); //设置id
            childrendiv.addClass('layui-nav-item layui-nav-left');
            if(i==0){
                childrendiv.addClass('layui-nav-itemed');
            }
            childrendiv.html(content);
            div.append(childrendiv);
            if (data.children) {
                mue.createsubLi(data.children,idname,pidname,`mue${data[idname]}`);
            }   
        },
    //创建树形布局
    createsubLi:function(data,idname,pidname,thisgrid) {
            // 父元素id
            const fid = thisgrid;
            const div = $('#'+fid);
            var content='';
            content = mue.create(data,idname,pidname,thisgrid);
            div.append(content);
               
        },
    create:function(data,idname,pidname,thisgrid) {
            var content='';
            content += '<dl id=sub'+thisgrid+' class="layui-nav-child ">';
            (eval(data)).forEach(d=>{
                content += '<dd data-name="" id=mue'+d[idname]+'><a';
                if(d.url=='#'){
                   content += ' href="'+d.url+'">';
                }else{
                  content += ' lay-href="'+d.code+d.url+'">';
                }
                if(d.icon){
                   content += '<i class="layui-icon">&#'+d.icon+'</i>';
                }
                content += d.title+'</a>';
                if(d.children){
                    content += mue.create(d.children,idname,pidname,`mue${d[idname]}`); 
                }
                content += '</dd>';
            })
            content +='</dl>'
            return content; 
    },
    //创建树形布局
    createtopLi:function(data,idname,pidname,thisgrid,i) {
            // 父元素id
            const fid = thisgrid;
            const div = $('#'+fid);
            var content='';
            if(data[pidname]==0){
                content +='<a href="javascript:;">';
                if(data.icon){
                content +='<i class="layui-icon">&#'+data.icon+'</i>';
                }
                content +='<cite>'+data.title+'</cite><i class="layui-icon layui-icon-down layui-nav-more"></i></a>';
            };
            var childrendiv=$('<li></li>'); //创建一个div
            childrendiv.attr('id', `mue${data[idname]}`); //设置id
            childrendiv.addClass('layui-nav-item');
            // if(i==0){
            //     childrendiv.addClass('layui-nav-itemed');
            // }
            childrendiv.html(content);
            div.append(childrendiv);
            if (data.children) {
                this.createsubtopLi(data.children,idname,pidname,`mue${data[idname]}`);
            }   
        },
    //创建树形布局
    createsubtopLi:function(data,idname,pidname,thisgrid) {
            // 父元素id
            const fid = thisgrid;
            const div = $('#'+fid);
            var content='';
            (eval(data)).forEach(d=>{

                content += '<dd class="layui-nav-child-top" data-name="" id=mue'+d[idname]+'><a';
                if(d.url=='#'){
                   content += ' href="'+d.url+'">';
                }else{
                  content += ' lay-href="'+d.code+d.url+'">';
                }
                if(d.icon){
                   content += '<i class="layui-icon">&#'+d.icon+'</i>';
                }
                content += d.title+'</a>';

                if(d.children){
                    this.createsubtopLi(d.children,idname,pidname,`mue${d[idname]}`); 
                }

            })
            var childrendiv=$('<dl></dl>'); //创建一个dl
            childrendiv.attr('id', `sub${thisgrid}`); //设置id
            childrendiv.addClass('layui-nav-child');
            childrendiv.html(content);
            div.append(childrendiv);   
             
        }

};
exports('mue', mue);
});

var $ = layui.$;
//菜单 模块
        $('.left_open i').mouseover(function(event) {
        $('.layui-open-menu').animate({left: '0'}, 300,function(){
        $('.layui-menu-right').show();
         });
        $('.left_open i').removeClass('layui-icon-spread-left');
        $('.left_open i').addClass('layui-icon-close');
    });
    $('.left_open i').click(function(event) {
        $('.layui-open-menu').animate({left: '-100%'}, 600);
        $('.layui-menu-right').hide();
        $('.left_open i').removeClass('layui-icon-close');
        $('.left_open i').addClass('layui-icon-spread-left');
    });
    $('.layui-menu-right').click(function(event) {
        $('.layui-open-menu').animate({left: '-100%'}, 600);
        $('.layui-menu-right').hide();
        $('.left_open i').removeClass('layui-icon-close');
        $('.left_open i').addClass('layui-icon-spread-left');
    });
    $('.lay_close_mue').click(function(event) {
        $('.layui-open-menu').animate({left: '-100%'}, 600);
        $('.layui-menu-right').hide();
        $('.left_open i').removeClass('layui-icon-close');
        $('.left_open i').addClass('layui-icon-spread-left');
    });

//菜单 子功能
$('.layui-menu-subclose').click(function(event) {
       if($('.layui-side-menu').css('left')=='0px'){
          $('.layui-side-menu').animate({left: '-220'}, 100);
          $('.layui-menu-subclose').animate({left: '0'}, 420);
          $('.layadmin-pagetabs').animate({left: '0'}, 100);
          $('.layui-body').animate({left: '0'}, 100);
          $('.layui-menu-subclose i').html('&#xe602;');
      }else{
          $('.layui-menu-subclose i').html('&#xe603;');
          $('.layui-side-menu').animate({left: '0px'}, 100);
          $('.layui-menu-subclose').animate({left: '220'}, 420);
          $('.layadmin-pagetabs').animate({left: '220'}, 100);
          $('.layui-body').animate({left: '220'}, 100);  
      }
    });


